import * as turf from '@turf/turf'
export class ReadFileDiamond4 {
    /**
     * 文件地址
     * @type {string}
     * @memberof _readFileDiamond4
     */
    filepath

    /**
     * 文件解析之后的数据
     * @memberof _readFileDiamond4
     */
    json = []

    /**
     * lon的格距
     * @memberof _readFileDiamond4
     */
    gjLon = 0

    /**
     * lat的格距
     * @memberof _readFileDiamond4
     */
    gjLat = 0

    constructor(data) {
        this.data = data
    }

    /**
     * 读取文件
     */
    async start() {
        const data = this.data
        const dataOnLine = this.data.dvalues
        this.gjLon = Number(Math.abs(Number(data.left) - Number(data.bottom)) / data.cols).toFixed(3)
        this.gjLat = Number((Math.abs(Number(data.top) - Number(data.right)) / data.rows).toFixed(3))
        const startLon = Number(data.left),
            startLat = Number(data.right)
        let lat = startLat
        let lng = startLon
        for (let i = 0; i < dataOnLine.length; i += data.cols) {
            ;(lat = Number(lat) + Number(this.gjLat)), (lng = startLon)
            const dataObj = dataOnLine.slice(i, i + data.cols)
            for (let i = 0; i < dataObj.length; i++) {
                ;(lng = Number(lng) + Number(this.gjLon)),
                    this.json.push({
                        lat: lat,
                        lon: lng,
                        val: dataObj[i]
                    })
            }
        }
    }

    /**
     * 获取json数据
     * @returns
     */
    getJson() {
        return this.json
    }

    setJson(data) {
        this.json = data
    }

    /**
     * 获取稀释之后的json数据
     * @param h 每间隔多少列抽一列
     * @param l 每间隔多少行抽一行
     * @returns
     */
    getXSJson(h, l) {
        let i = 0,
            z = 0,
            b = true
        const json = []
        for (let j = 0; j < this.json.length; j++) {
            if (i == 0 && b) {
                json.push(this.json[j])
            }
            i++
            z++
            if (i == h || z % this.lonLineNum == 0) {
                // 每间隔多少列抽一列
                i = 0
            }
            if (z == this.lonLineNum * l) {
                // 每间隔多少行抽一行
                b = !b
                z = 0
            }
            if (z == this.lonLineNum && b) {
                b = !b
            }
        }
        return json
    }

    getColor(num = 0, level, color) {
        let rgb = 'rgba(255,255,255,0)'
        for (let i = 0; i < level.length; i++) {
            if (level[i] <= num && num < level[i + 1]) {
                rgb = 'rgba(' + color[i][0] + ',' + color[i][1] + ',' + color[i][2] + ',1)'
                break
            }
        }
        return rgb
    }

    getBreaks() {
        let max = 0,
            min = 0
        const data = this.json

        for (const p of data) {
            if (parseFloat(p.val) < min || min == null) {
                min = parseFloat(p.val)
            }
            if (parseFloat(p.val) > max || max == null) {
                max = parseFloat(p.val)
            }
        }
        const breaks = []
        for (let i = parseInt(min + '') - 1; i < parseInt(max + '') + 1; i++) {
            breaks.push(i)
        }
        return breaks
    }

    getIsolines(breaks = []) {
        const json = this.getJson()
        const jsonPoint = json.map(t => [t.lon, t.lat])
        const point = turf.points(jsonPoint)
        point.features.map(t => {
            t.properties = {
                z: parseFloat(
                    json.find(j => j.lon == t.geometry.coordinates[0] && j.lat == t.geometry.coordinates[1])?.val || '0'
                )
            }
        })
        if (breaks.length == 0) breaks = this.getBreaks()
        const lines = turf.isolines(point, breaks, { zProperty: 'z' })
        return lines
    }

    getIsoBands(breaks = []) {
        const json = this.getJson()
        const jsonPoint = json.map(t => [t.lon, t.lat])
        const point = turf.points(jsonPoint)
        point.features.map(t => {
            t.properties = {
                value: parseFloat(
                    json.find(j => j.lon == t.geometry.coordinates[0] && j.lat == t.geometry.coordinates[1])?.val || '0'
                )
            }
        })
        if (breaks.length == 0) breaks = this.getBreaks()
        const isobands = turf.isobands(point, breaks, { zProperty: 'value' })
        return isobands
    }

    getIsoColor(z, level, color) {
        let rgb = 'rgba(255,255,255,0)'
        for (let i = 0; i < level.length - 1; i++) {
            if (i < level.length - 1) {
                if (level[i] + '-' + level[i + 1] == z) {
                    rgb = 'rgba(' + color[i][0] + ',' + color[i][1] + ',' + color[i][2] + ',1)'
                }
            }
        }
        return rgb
    }
}
